/**
 * 
 */
package org.wangh.csv.reader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;

import com.csvreader.CsvReader;

/**
 * @author wang
 *
 */
public class CSVReader {
	
	private final String path;
	private final Charset charset;
	private final char delimiter;
	
	private CsvReader reader;
	
	public CSVReader(String path){
		this(path, null, null);
	}
	
	public CSVReader(String path, Character delimiter){
		this(path, delimiter, null);
	}
	
	public CSVReader(String path, Character delimiter, Charset charset){
		this.path = path;
		this.charset = charset;
		this.delimiter = delimiter;
		initInputFile();
	}
	
	private void initInputFile(){
		try {
			File file = new File(path);
			FileInputStream in = new FileInputStream(file);
			
			if(charset != null){
				reader = new CsvReader(in, charset);
			}else{
				reader = new CsvReader(path);
			}
			
			if(delimiter != '\0'){
				reader.setDelimiter(delimiter);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
	}
	
	public String[] getHeader(){
		try {
			reader.readHeaders();
			return reader.getHeaders();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
		
}
